package com.meirimoney.app;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.meirimoney.app.control.MenuBar;
import com.meirimoney.task.BalanceTask;

public class MainActivity extends BaseAccount implements OnClickListener
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.initializeUI();
		this.setTitle("免费彩金");
		this.registrationEvent();
	}

	@Override
	public void done()
	{
		this.setBalance();
	};

	@Override
	protected void onStart()
	{
		super.onStart();
		if (this.getUser().getLoginStatus())
		{
			this.setBalance();
		}
	};

	/***
	 * 初始化当前界面的UI
	 */
	private void initializeUI()
	{
		this.setContentView(R.layout.mrm_default);
		if (null == this.navigation)
		{
			this.navigation = (MenuBar) this.findViewById(R.id.head);
		}
		if (null == this.refreshBtn)
		{
			this.refreshBtn = (TextView) this.findViewById(R.id.refreshBtn);
			this.refreshBtn.setOnClickListener(this);
		}
		if (null == this.progressBar)
		{
			this.progressBar = (ProgressBar) this
					.findViewById(R.id.progressBar);
		}
	}

	/***
	 * 设置余额
	 */
	public void setBalance()
	{
		if (null == this.balancePanel)
		{
			this.balancePanel = (TextView) this.findViewById(R.id.balance);
		}
		if (this.task != null)
		{
			AsyncTask.Status status = this.task.getStatus();
			if (status != AsyncTask.Status.FINISHED)
			{
				return;
			}
		}
		this.task = new BalanceTask(this, this.balancePanel);
		this.task.execute(this.getUser().getToken());
	}

	/***
	 * 注册任务按钮的单击事件的监听方法
	 */
	private void registrationEvent()
	{
		if (null == this.youmiBtn)
		{
			this.youmiBtn = (TextView) this.findViewById(R.id.youmi);
		}
		this.youmiBtn.setOnClickListener(this);
		if (null == this.alipayWithdrawBtn)
		{
			this.alipayWithdrawBtn = (TextView) this
					.findViewById(R.id.alipayWithdraw);
		}
		this.alipayWithdrawBtn.setOnClickListener(this);
		if (null == this.phoneRechargeBtn)
		{
			this.phoneRechargeBtn = (TextView) this
					.findViewById(R.id.phoneRecharge);
		}
		this.phoneRechargeBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		if (v.getId() == R.id.youmi)
		{
			this.startActivity(new Intent(this, AdvertisingPanel.class));
		} else if (v.getId() == R.id.phoneRecharge
				|| v.getId() == R.id.alipayWithdraw)
		{
			this.startActivity(new Intent(this, CommodityList.class));
		} else if (v.getId() == R.id.refreshBtn)
		{
			this.refreshBalance();
		}
	}

	/***
	 * 置位等待状态
	 */
	public void refreshBalance()
	{
		this.refreshBtn.setVisibility(View.GONE);
		this.progressBar.setVisibility(View.VISIBLE);
		this.setBalance();
	}

	private BalanceTask task;

	/***
	 * 余额
	 */
	private TextView balancePanel;

	/***
	 * 有米任务按钮
	 */
	private TextView youmiBtn;

	/***
	 * 提现按钮
	 */
	private TextView alipayWithdrawBtn;

	/***
	 * 花费充值
	 */
	private TextView phoneRechargeBtn;

	/**
	 * 刷新按钮
	 */
	private TextView refreshBtn;

	public TextView getRefreshBtn()
	{
		return refreshBtn;
	}

	/**
	 * 进度条
	 */
	private ProgressBar progressBar;

	public ProgressBar getProgressBar()
	{
		return progressBar;
	}
}
